<?php

/**
 *
 * @author yupoxiong<i@yupoxiong.com>
 */


namespace app\api\controller;

use app\common\model\Adjust;
use app\common\model\Channel;
use app\common\model\Page;
use app\common\model\Promotion;
use app\common\model\User;
use think\facade\Db;
use think\facade\Log;
use think\Request;
use think\response\Json;

class IndexController
{
    public function index(Request $request)
    {
        // $appcode = $request->get('appcode');
        $adcode = $request->param('adcode');
        $usercode = $request->param('usercode');
        if (!isset($adcode) || !isset($usercode)) {
            return json(['success' => 0, 'value' => [], 'message' => '参数错误', 'errCode' => 400]);
        }
        //判断adjust数据
        $adjustModel = new Adjust();
        $adarr = $adjustModel->where('adid', $adcode)->order('id desc')->find();
        if (!($adarr)) {
            $getid = $adjustModel->insert([
                'adid' => $adcode,
                'create_time' => time(),
            ], false, true);
        } else {
            $getid = $adarr['id'];
        }
        Log::write('app-得到adid后，操作adjust:' . $getid . ' 参数：' . json_encode($request->param()), 'info');

        if ($usercode) {
            User::where('usercode', $usercode)->update([
                'adid' => $adcode,
            ]);
        }
        return json(['success' => 1, 'value' => ['isvalid' => 0, 'isSubscriber' => 0, 'url' => ''], 'message' => 'success', 'errCode' => 200]);
    }
    /**
     * 接收adjust返回信息
     */
    public function adjust(Request $request)
    {
        $content = json_encode($request->param());
        Log::write('adjust返回信息：' . $content, 'info');
        $adid = $request->param('adid');
        $adjustModel = Db::table('adjust');
        $adarr = $adjustModel->where('adid', $adid)->order('id desc')->find();
        if (!$adarr) {
            $adjustModel->insert([
                'adid' => $adid,
                'country' => $request->param('country'),
                'tracker_name' => $request->param('network_name', 'Organic'),
                'content' => $content,
                'update_time' => time(),
            ]);
        } else {
            Db::table('adjust')->where('adid', $adid)->update([
                'country' => $request->param('country'),
                'tracker_name' => $request->param('network_name', 'Organic'),
                'content' => $content,
                'update_time' => time(),
            ]);
        }
        return 'success';
    }

    public function adjust_event(Request $request)
    {
        $content = json_encode($request->param());
        Db::table('adjust_event')->insert([
            'content' => $content,
            'adid' => $request->param('adid'),
            'create_time' => time(),
        ]);
        return 'success';
    }
    /**
     * 调查问卷逻辑
     */
    public function wjdc(Request $request)
    {
        $usercode = $request->param('usercode');
        if ($usercode) {
            $user_data = User::where('usercode', $usercode)->find();
            $adid = $user_data['adid'];
            $appid = $user_data['appcode'];
        } else {
            $adid = $request->param('adid');
            $appid = $request->param('appid');
        }
        Log::write('wjdc-0-请求url,参数：' . json_encode($request->param()).',adid='.$adid.' appid'.$appid, 'info');
        $channel_arr = Channel::where('number', $appid)->find(); //判断包是否错误
        Log::write('wjdc-1-请求url，channel包信息：' . json_encode($channel_arr), 'info');

        $url = $channel_arr['url'].'?usercode=' . $usercode.'&appid='.$appid; //A面地址
        //判断adjust数据
        $adjustModel = Db::table('adjust');
        $adarr = $adjustModel->where('adid', $adid)->order('id desc')->find();

        if (!($adarr)) { //还未获取adjust数据
            Log::write('wjdc-1-adjust无数据跳a面：' , 'info');
            echo '<script>window.location.href="' . $url . '";</script>';
            die;
        }
        if ($adarr['tracker_name'] == 'Organic' || $adarr['tracker_name'] == '') { //如果自然流量
            Log::write('wjdc-1-adjust归因问题，跳a面：'.json_encode($adarr) , 'info');
            echo '<script>window.location.href="' . $url . '";</script>';
            die;
        }
        $promotion_arr = Promotion::where(['channel_id' => $channel_arr['id'], 'country' => $adarr['country']])->find(); //获取推广信息
        Log::write('wjdc-2-请求url，promotion推广信息：' . json_encode($promotion_arr), 'info');
        if (!$promotion_arr) {
            echo '<script>window.location.href="' . $url . '";</script>';
            die;
        }
        $page_arr = Page::where(['id' => $promotion_arr['page_id']])->find(); //根据国家和包信息读取落地页
        Log::write('wjdc-3-请求url，page信息：' . json_encode($page_arr), 'info');
        if ($page_arr) {
            $url = 'https://www.' . $page_arr['domain'] . '/h_' . $page_arr['keys'] . '.html?usercode=' . $usercode.'&appid='.$appid;  //B面地址
        }
        echo '<script>window.location.href="' . $url . '";</script>';
        die;
    }
}
